package com.symaster.common.swing.element.collapse.item;

import com.symaster.common.image.util.ImageUtil;
import com.symaster.common.swing.element.button.RotateButton;
import com.symaster.common.swing.util.SwingUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.InputStream;

/**
 * @author yinmiao
 * @version 2022-08-26 16:46
 */
public class Row<T extends JComponent> extends JComponent {
    private JPanel root_panel;
    private JPanel upper;
    private JPanel lower;
    private JPanel icon_area;
    private JPanel bg1;
    private JPanel bg2;
    private JPanel bg3;
    private JPanel bg4;
    private T data;
    private boolean closed = true;
    private RowSpreadListener<T> rowSpreadListener;
    private RotateButton rotateButton;
    private int oldHeight = 45;
    private final MyMouseListener myMouseListener = new MyMouseListener(this);

    public Row() {
        initPage();
    }

    private void initPage() {
        setLayout(new BorderLayout());
        add(root_panel);

        rotateButton = new RotateButton();
        rotateButton.setMarge(15);

        try {
            InputStream inputStream = SwingUtil.loadStream("arrow-right_200X200.png");
            rotateButton.setMat(ImageUtil.toMat(inputStream));
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }

        rotateButton.addActionListener(this::spread);
        bg1.addMouseListener(myMouseListener);
        bg2.addMouseListener(myMouseListener);
        bg3.addMouseListener(myMouseListener);
        bg4.addMouseListener(myMouseListener);

        icon_area.add(rotateButton);

        rotateButton.setIcon(SwingUtil.loadIconEnabledCache("arrow-right_200X200.png", 15, 15));
    }

    @Override
    public void setBackground(Color bg) {
        super.setBackground(bg);
        rotateButton.setBackground(bg);
        upper.setBackground(bg);
        lower.setBackground(bg);
        bg1.setBackground(bg);
        bg2.setBackground(bg);
        bg3.setBackground(bg);
        bg4.setBackground(bg);
    }

    private void spread(ActionEvent actionEvent) {
        lower.removeAll();
        if (closed) {
            if (rowSpreadListener != null) {
                JComponent handler = rowSpreadListener.handler(this);
                if (handler != null) {
                    Dimension size = handler.getPreferredSize();
                    lower.add(handler, BorderLayout.CENTER);
                    lower.setVisible(true);
                    lower.setPreferredSize(size);
                    Dimension size1 = getSize();
                    this.oldHeight = size1.height;
                    Dimension dimension = new Dimension(size1.width, size1.height + size.height);
                    setPreferredSize(dimension);
                    this.repaint();
                }
            }
            rotateButton.rotate(90);
            closed = false;
        } else {
            Dimension size1 = getSize();
            setPreferredSize(new Dimension(size1.width, oldHeight));
            rotateButton.rotate(0);
            closed = true;
        }
        lower.setVisible(!closed);
        if (rowSpreadListener != null) {
            rowSpreadListener.over();
        }
    }

    public void setData(T data) {
        this.data = data;
        upper.removeAll();
        data.addMouseListener(myMouseListener);
        upper.add(data);
    }

    public T getData() {
        return data;
    }

    public void setRowSpreadListener(RowSpreadListener<T> rowSpreadListener) {
        this.rowSpreadListener = rowSpreadListener;
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        root_panel = new JPanel();
        root_panel.setLayout(new BorderLayout(0, 0));
        root_panel.setPreferredSize(new Dimension(0, 0));
        lower = new JPanel();
        lower.setLayout(new BorderLayout(0, 0));
        lower.setVisible(false);
        root_panel.add(lower, BorderLayout.CENTER);
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new BorderLayout(0, 0));
        panel1.setPreferredSize(new Dimension(0, 45));
        root_panel.add(panel1, BorderLayout.NORTH);
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new BorderLayout(0, 0));
        panel2.setPreferredSize(new Dimension(45, 0));
        panel1.add(panel2, BorderLayout.WEST);
        bg1 = new JPanel();
        bg1.setLayout(new BorderLayout(0, 0));
        bg1.setPreferredSize(new Dimension(0, 0));
        panel2.add(bg1, BorderLayout.WEST);
        bg2 = new JPanel();
        bg2.setLayout(new BorderLayout(0, 0));
        bg2.setPreferredSize(new Dimension(20, 0));
        panel2.add(bg2, BorderLayout.EAST);
        bg3 = new JPanel();
        bg3.setLayout(new BorderLayout(0, 0));
        bg3.setPreferredSize(new Dimension(0, 10));
        panel2.add(bg3, BorderLayout.NORTH);
        bg4 = new JPanel();
        bg4.setLayout(new BorderLayout(0, 0));
        bg4.setPreferredSize(new Dimension(0, 10));
        panel2.add(bg4, BorderLayout.SOUTH);
        icon_area = new JPanel();
        icon_area.setLayout(new BorderLayout(0, 0));
        panel2.add(icon_area, BorderLayout.CENTER);
        upper = new JPanel();
        upper.setLayout(new BorderLayout(0, 0));
        panel1.add(upper, BorderLayout.CENTER);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return root_panel;
    }

    public interface RowSpreadListener<T extends JComponent> {
        JComponent handler(Row<T> row);

        void over();
    }

    private static class MyMouseListener implements MouseListener {

        private final Row<?> row;

        public MyMouseListener(Row<?> row) {
            this.row = row;
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            row.spread(null);
        }

        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }
    }
}
